package com.rdxer.service;

import com.rdxer.core.utils.IdWorker;
import com.rdxer.dataview.PaperQuestionDV;
import com.rdxer.model.PaperQuestionRelation;
import com.rdxer.model.Question;
import com.rdxer.model.QuestionOption;
import com.rdxer.repository.PaperQuestionRelationRepository;
import com.rdxer.repository.QuestionRepository;
import lombok.Data;
import lombok.Getter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Department 服务层
 */
@Service
@Data
@Getter
public class PaperQuestionRelationService implements CRUDPageServiceInterface<PaperQuestionRelation, String> {


    @Autowired
    private PaperQuestionRelationRepository repository;
    @Autowired
    private QuestionRepository questionRepository;
    @Autowired
    private QuestionOptionService questionOptionService;

    @Autowired
    private IdWorker idWorker;

    public List<PaperQuestionDV> findQuestionByPaperId(String id) {
        List<PaperQuestionRelation> byPaperId = repository.findAllByPaperIdOrderByIdx(id);
        List<Question> allById = questionRepository
                .findAllById(byPaperId.stream()
                        .map(v -> v.getQuestionId())
                        .collect(Collectors.toList()
                        )
                );
        List<PaperQuestionDV> list = new ArrayList<>();
        for (PaperQuestionRelation relation : byPaperId) {
            PaperQuestionDV r = new PaperQuestionDV();
            BeanUtils.copyProperties(relation, r);

            for (Question question : allById) {
                if (r.getQuestionId().equals(question.getId())) {
                    r.setQuestion(question);
                    break;
                }
            }

            list.add(r);
        }

        for (PaperQuestionDV dv : list) {
            String questionId = dv.getQuestionId();
            List<QuestionOption> allByQuestionId = questionOptionService.findAllByQuestionId(questionId);
            allByQuestionId.sort((v,v2) -> {
                return v.getIdx() - v.getIdx();
            });
            dv.setQuestionOption(allByQuestionId);
        }
        return list;
    }

    public void upIndex(String relationId, int idx) {
        PaperQuestionRelation byId = repository.findById(relationId).get();
        byId.setIdx(idx);
        repository.save(byId);
    }

    public void removeRelation(String relationId) {
        PaperQuestionRelation byId = findById(relationId);
        questionOptionService.deleteByQuestionId(byId.getQuestionId());
        questionRepository.deleteById(byId.getQuestionId());
        delete(relationId);
        List<PaperQuestionRelation> relationList = repository.findAllByPaperIdOrderByIdx(relationId);

        for (int i = 0; i < relationList.size(); i++) {
            PaperQuestionRelation relation = relationList.get(i);
            relation.setIdx(i+1);
            save(relation);
        }
    }

    public void questionMoveToIndex(String relationId, int idx) {
        PaperQuestionRelation currRelation = findById(relationId);
        List<PaperQuestionRelation> relationList = repository
                .findAllByPaperIdOrderByIdx(currRelation.getPaperId());
        relationList.removeIf(v->
                Objects.equals(v.getId(), currRelation.getId()));
        relationList.add(idx-1,currRelation);

        for (int i = 0; i < relationList.size(); i++) {
            PaperQuestionRelation relation = relationList.get(i);
            relation.setIdx(i+1);
            save(relation);
        }
    }
}
